﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BestTrader.Cms.IServices;
using BestTrader.Cms.ViewModels;
using BestTrader.Cms.ViewModels.WebApi;
using BestTrader.Cms.WebApi.Validation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BestTrader.Cms.WebApi.Controllers.v1
{

    [ApiVersion("1.0")]
    [Route("api/v{version:apiVersion}/[controller]")]
    [ApiController]
    public class HistoryCashController : BaseController
    {
        private readonly IHistory_CashChangesService _service;

        public HistoryCashController(IHistory_CashChangesService service)
        {
            _service = service;
        }
        /// <summary>
        /// [10001] 获取风软历史出入金记录
        /// </summary>
        /// <param name="input">出入金记录</param>
        /// <remarks>
        /// INPUT: /PageSearchInput
        /// {
        /// CurrentId=96  //当前user ID
        /// ManagerId=96  //搜索用户Id  
        ///  Start=         //开始时间   
        /// End=  //结束时间    
        /// Key=  //关键字搜索,   
        /// Limit= //每页多少条 
        /// Page=  //页码
        /// State =////状态 1.入金;2.出金
        /// }
        /// OUTPUT /LoginOutput
        /// {
        ///  "id": 48,
        ///  "cashChangesID": 766, 
        ///  "account_ID": 479, 
        ///  "action_Time": "2019-04-08T23:28:15",  //操作时间
        ///  "cash_State": 1,  //状态 1.入金;2.出金
        ///  "amount": 500,      //金额
        ///  "currencyCode": "USD",  //货币单位
        ///  "remark": "用户入金",
        ///  "accountName": "QS10053101",
        ///  "addTime": "2019-04-10T16:39:37.737",
        ///  "currencyCodeName": "$", //货币符号
        ///  "userName": "QS100531",
        ///  "realName": "田富民" 
        /// }
        /// </remarks>
        /// <returns>LoginOutput</returns>
        [HttpPost("GetHistoryCashRecord")]
#if !DEBUG
        [Authorize]
#endif
        public JsonResult GetHistoryCashRecord(PageSearchInput input)
        {
            var result = new BaseResult { Success = false };
            var data = new TableDataModel { data = "", count = 0 };
            var UserId = input.CurrentId.ToString();
            var RoleId = input.RoleId.ToString();

            PageSearchValidation validationRules = new PageSearchValidation();
            ValidationResult results = validationRules.Validate(input);

            if (results.IsValid)
            {
                if (input.Limit == 0)
                {
                    input.Limit = 10;
                }
                if (input.Page == 0)
                {
                    input.Page = 1;
                }
                var model = new HistoryCashRequestModel()
                {
                    Start = input.Start,
                    End = input.End,
                    Key = input.Key,
                    Limit = input.Limit,
                    Page = input.Page,
                    State = input.State > 0 ? input.State : null,
                    ManagerId = input.ManagerId > 0 ? input.ManagerId : null
                };

                var dataResult = _service.LoadData(model, UserId, RoleId);
                var dataTotal = _service.GetTotalHistoryCashByConditions(model, UserId, RoleId);

                var returnData = new
                {
                    code = dataResult.code,
                    data = dataResult.data,
                    count = dataResult.count,
                    totalData = dataTotal.data
                };
                result.Success = true;
                return Json<Object>(returnData, result.Success, result.ResultCode, result.ResultMsg);
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
                result.ResultMsg = results.ToString("||");
            }
            return Json<Object>(data, result.Success, result.ResultCode, result.ResultMsg);

        }




    }

}